package com.thinkgem.jeesite.modules.feedback.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.building.entity.Room;
import com.thinkgem.jeesite.modules.building.service.BuildingService;
import com.thinkgem.jeesite.modules.building.service.RoomService;
import com.thinkgem.jeesite.modules.feedback.entity.Feedback;
import com.thinkgem.jeesite.modules.feedback.service.FeedbackService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

@Controller
@RequestMapping(value="${adminPath}/feedback")
public class FeedbackController extends BaseController{
	@Autowired
	private FeedbackService feedbackService;
	@Autowired
	private RoomService roomservice;
	@Autowired
	private BuildingService buildingservice;
	/**
	 * 找到所有的反馈
	 * */
	@RequestMapping(value="/feedbackAllList")
	public String feedbackList(HttpServletRequest request,HttpServletResponse response,Model model){
		Page<Feedback> page = feedbackService.findPage(new Page<Feedback>(request,response),new Feedback());  //所有的未完成反馈
		model.addAttribute("page", page);
		return "modules/feedback/feedbackList";
	}
	/**
	 * 找到管理员管理下的反馈
	 * */
	@RequestMapping(value="/MyAdminFeedbackList")
	public String MyAdminFeedbackList(HttpServletRequest request,HttpServletResponse response,Model model){
		Page<Feedback> page = feedbackService.findMyFeedbackList(new Page<Feedback>(request,response),new Feedback());
		model.addAttribute("page", page);
		return "modules/feedback/MyAdminFeedbackList";
	}
	/**
	 * 找到自己的反馈
	 * */
	@RequestMapping(value="/MyOwnFeedback")
	public String MyOwnFeedback(HttpServletRequest request,HttpServletResponse response,Model model){
		Page<Feedback> page = feedbackService.findOwnerFeedback(new Page<Feedback>(request,response),new Feedback());
		model.addAttribute("page", page);
		return "modules/feedback/MyOwnFeedbackList";
	}
	/**
	 * 跳转到反馈页面
	 * */
	@RequestMapping(value="/FeedbackForm")
	public String FeedbackForm(Model model){
		if(!UserUtils.getUser().getIsUser().equals("1")){
			addMessage(model, "管理员无权进入用户模块");
			return "redirect:"+Global.getAdminPath()+"/sys/user/info";
		}
		Room room = roomservice.findRoomByOwnerId(UserUtils.getUser().getId());
		model.addAttribute("feedback", new Feedback());
		model.addAttribute("room", room);
		return "modules/feedback/feedbackForm";
	}
	/**
	 * 保存反馈
	 * */
	@RequestMapping(value="/save")
	public String FeedbackForm(Feedback feedback,Model model){
		Date now = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Room room = roomservice.findRoomByOwnerId(UserUtils.getUser().getId());
		feedback.setId(UUID.randomUUID().toString());
		feedback.setRoom(room);
		feedback.setCreateTime(format.format(now).toString());
		feedback.setIsDeal("1");
		feedbackService.insertFeedback(feedback);
		return "redirect:"+Global.getAdminPath()+"/sys/user/info";
	}
	/**
	 * 更改反馈状态
	 * */
	@RequestMapping(value="/updateFeedback")
	public String updateFeedback(@RequestParam("id")String id,Model model){
		Feedback feedback = new Feedback();
		feedback.setId(id);
		feedback.setIsDeal("0");
		feedbackService.updateState(feedback);
		return "redirect:"+Global.getAdminPath()+"/sys/user/info";
	}
}
